﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GeneralClasses;

namespace Interface
{
    public partial class frmDepartments : Form
    {
        Form opener;
        List<Department> departments;
        Department selectedDepartment;

        public frmDepartments(Form Opener)
        {
            opener = Opener;
            InitializeComponent();
        }

        void refresh()
        {
            txtAddDepartmentName.Text = "";
            txtEditDepartmentName.Text = "";

            selectedDepartment = null;

            departments = Department.loadAllDepartments();

            lstboxDepartments.Items.Clear();
            foreach (Department item in departments)
            {
                lstboxDepartments.Items.Add(item.Name);
            }
        }

        private void frmDepartments_FormClosing(object sender, FormClosingEventArgs e)
        {
            opener.Show();
        }

        private void frmDepartments_Shown(object sender, EventArgs e)
        {
            refresh();
        }

        private void btnAddDepartment_Click(object sender, EventArgs e)
        {
            if (txtAddDepartmentName.Text == "")
            {
                MessageBox.Show("Please enter a name for the department!");
                txtAddDepartmentName.Focus();
            }
            else
            {
                selectedDepartment = new Department();

                selectedDepartment.Name = txtAddDepartmentName.Text;

                if (selectedDepartment.saveData() > 0)
                {
                    refresh();
                }
                else
                {
                    MessageBox.Show("Problem adding department!");
                    selectedDepartment = null;
                }
            }
        }

        private void btnDeleteDepartment_Click(object sender, EventArgs e)
        {
                if (selectedDepartment != null)
            {
                DialogResult result = MessageBox.Show("Are you sure you want to delete department " +
                    lstboxDepartments.SelectedItems[0] + "?", "Delete?", MessageBoxButtons.YesNo);

                if (result == DialogResult.Yes)
                {
                    if (selectedDepartment.removeData() == 0)
                    {
                        refresh();
                    }
                    else
                    {
                        MessageBox.Show("Problem deleting department!");
                    }
                }
            }

        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnEditDepartment_Click(object sender, EventArgs e)
        {
            if (selectedDepartment != null)
            {
                selectedDepartment.Name = txtEditDepartmentName.Text;

                if (selectedDepartment.saveData() > 0)
                {
                    refresh();
                }
                else
                {
                    MessageBox.Show("Problem editing deparment.");
                }
            }
        }

        private void lstboxDepartments_SelectedIndexChanged(object sender, EventArgs e)
        {
            foreach (Department item in departments)
            {
                if (item.Name == lstboxDepartments.SelectedItem.ToString())
                {
                    selectedDepartment = item;
                    break;
                }
            }

            if (selectedDepartment != null)
            {
                txtEditDepartmentName.Text = selectedDepartment.Name;
            }
        }

        private void txtAddDepartmentName_TextChanged(object sender, EventArgs e)
        {
            Input_Constraint.keepOnly(txtAddDepartmentName, true, true, true, true, null);
        }

        private void txtEditDepartmentName_TextChanged(object sender, EventArgs e)
        {
            Input_Constraint.keepOnly(txtEditDepartmentName, true, true, true, true, null);
        }
    }
}
